import turtle

# 画x标尺
def draw_x_scale(pen: turtle.Turtle, x):
    pen.teleport(x, -5)
    pen.setheading(0)
    pen.left(90)
    pen.forward(10)
    pen.teleport(x, -32)
    pen.write(x, align="center", font=("Arial", 18, "normal"))

# 画y标尺
def draw_y_scale(pen: turtle.Turtle, y):
    pen.teleport(5, y)
    pen.setheading(90)
    pen.left(90)
    pen.forward(10)
    pen.teleport(-24, y - 10)
    pen.write(y, align="center", font=("Arial", 18, "normal"))

def draw_coord(pen: turtle.Turtle, width: int = 1200, height: int = 800, step: int = 100):
    # 用灰色画坐标轴
    pen.color("silver")
    # 画x轴
    pen.teleport(0 - width / 2, 0)
    pen.setheading(0)
    pen.forward(width)
    # 画箭头
    pen.left(150)
    pen.forward(10)
    pen.backward(10)
    pen.right(300)
    pen.forward(10)
    pen.teleport(width / 2, 10)
    pen.write("x", align="center", font=("Arial", 32, "italic"))

    # 画y轴
    pen.teleport(0, 0 - height / 2)
    pen.setheading(90)
    pen.forward(height)
    # 画箭头
    pen.left(150)
    pen.forward(10)
    pen.backward(10)
    pen.right(300)
    pen.forward(10)
    pen.teleport(20, height / 2 - 12)
    pen.write("y", align="center", font=("Arial", 32, "italic"))

    x_lines = int(height / 2 / step)
    y_lines = int(width / 2 / step)
    # 画x轴正数标尺
    for i in range(y_lines + 1):
        draw_x_scale(pen, i * step)
    # 画x轴负数标尺
    for i in range(1, y_lines + 1):
        draw_x_scale(pen, -i * step)

    # 画y轴正数标尺
    for i in range(1, x_lines + 1):
        draw_y_scale(pen, i * step)
    # 画y轴负数标尺
    for i in range(1, x_lines + 1):
        draw_y_scale(pen, -i * step)

    # 画横向参考线
    pen.color("snow2")
    pen.setheading(0)
    for i in range(-x_lines + 1, x_lines):
        if i == 0:
            continue
        pen.teleport(0 - width / 2, i * step)
        pen.forward(width)

    # 画纵向参考线
    pen.setheading(90)
    for i in range(-y_lines + 1, y_lines):
        if i == 0:
            continue
        pen.teleport(i * step, 0 - height / 2)
        pen.forward(height)

    # 回到原点
    pen.teleport(0, 0)
    pen.setheading(0)
    pen.color("black")

# 步长
step = 100
width = step * 2 * 6
height = width * 2 / 3

# 设置画布
screen = turtle.getscreen()
screen.title("笛卡儿坐标系（Cartesian coordinate system），也称直角坐标系（rectangular coordinate system）")
screen.setup(width + 200, height + 200)
screen.screensize(width, height)

# 设置画笔
pen = turtle.getpen()
pen.shape("turtle")
pen.speed(0) # 速度放到最快

def draw_coordinate():
    draw_coord(pen, width, height, step)

if __name__ == '__main__':
    draw_coordinate()
    screen.mainloop()